/*
 * L9InputStream.h
 *
 *  Created on: 2011-10-18
 *      Author: baixp
 */

#ifndef L9INPUTSTREAM_H_
#define L9INPUTSTREAM_H_
#include <FBase.h>
#include <FIo.h>
#include <fstream>
#include <string>
using namespace Osp::Base;
using namespace Osp::Io;
using namespace std;
namespace lib9bada {
class L9InputStream {
public:
	/**
	 * 根据文件来创建输入流
	 * @param file String
	 */
	L9InputStream(const char* file);
	/**
	 * 根据字节数组来创建输入流
	 * @param const byte* arr
	 * @param int off
	 * @param int len
	 */
	L9InputStream(const byte* arr, int off, int len);
	/**
	 * 输入流的拷贝构造函数，复制后初始化输入流内部指针
	 * @param const L9InputStream& in
	 */
	L9InputStream(const L9InputStream& in);
	/**
	 * 重载输入流=操作符,往往和拷贝构造函数一起定义
	 * @param const L9InputStream& in
	 */
	L9InputStream& operator=(const L9InputStream& in);
	/**
	 * 析构函数，释放资源
	 */
	virtual ~L9InputStream();
	/**
	 * 按照由低到高的从字节数据读取将int，占4个字节
	 * @param arr byte*
	 * @param off int
	 * @return int
	 */
	static int readIntLow(byte* arr, int off) {
		/*
		 //这一句话有问题，off++产生的副左右，bcb居然从右边开始计算,注意java中这句话就是正确的
		 int i = ((arr[off++] & 0xFF)) +
		 ((arr[off++] & 0xFF) << 8) +
		 ((arr[off++] & 0xFF) << 16) +
		 ((arr[off++] & 0xFF) << 24);
		 */
		int i = ((arr[off] & 0xFF)) + ((arr[off + 1] & 0xFF) << 8) + ((arr[off + 2]
				& 0xFF) << 16) + ((arr[off + 3] & 0xFF) << 24);
		if ((arr[off + 3] & (1 << 7)) != 0) { //符号为1表示负数，注意内存中使用的补码表示
			i = i - 1;
			i = ~i;
			i = -i;
		}

		return i;
	}

	/**
	 * 按照由低到高的从字节数据读取将long,占8个字节
	 * @param arr byte*
	 * @param off int
	 * @return long
	 */
	static long readLongLow(byte* arr, int off) {
		long i = ((long) (arr[off] & 0xFF)) + (((long) (arr[off + 1] & 0xFF)) << 8)
				+ (((long) (arr[off + 2] & 0xFF)) << 16) + (((long) (arr[off + 3]
				& 0xFF)) << 24) + (((long) (arr[off + 4] & 0xFF)) << 32)
				+ (((long) (arr[off + 5] & 0xFF)) << 40) + (((long) (arr[off + 6]
				& 0xFF)) << 48) + (((long) (arr[off + 7] & 0xFF)) << 56);
		if ((arr[off + 7] & (1 << 7)) != 0) { //符号为1表示负数，注意内存中使用的补码表示
			i = i - 1;
			i = ~i;
			i = -i;
		}
		return i;
	}

	/**
	 * 按照由低到高的从字节数据读取将Short，占2个字节
	 * @param arr byte*
	 * @param off int
	 * @return int
	 */
	static short readShortLow(byte* arr, int off) {
		int i = ((arr[off] & 0xFF) + ((arr[off + 1] & 0xFF) << 8));
		if ((arr[off + 1] & (1 << 7)) != 0) { //符号为1表示负数，注意内存中使用的补码表示
			i = i - 1;
			i = ~i;
			i = -i;
		}
		return (short) i;
	}
	bool readBoolean();
	byte readByte();
	int readByte(bool bSigned);
	byte* readBytesN(int len);
	short readShort();
	int readInt();
	long readLong();
	String readString();
	byte* getBytesN();
	void skipBytes(int nBytes);
	int getOff();
	void setOff(int off);
	int getSize();
private:
    byte* _buf;
    int _off;
    int _size;
};
};
#endif /* L9INPUTSTREAM_H_ */
